package com.lingo.mgr.service.impl;

import cn.hutool.core.lang.UUID;
import com.lingo.common.utils.DateUtils;
import com.lingo.common.utils.spring.SpringUtils;
import com.lingo.mgr.listener.IseClientListener;
import com.lingo.mgr.service.IAudioService;
import com.lingo.mgr.service.OpenAiService;
import com.lingo.mgr.websocket.LingoWebSocket;
import com.unfbx.chatgpt.OpenAiClient;
import com.unfbx.chatgpt.entity.Tts.TextToSpeech;
import com.unfbx.chatgpt.entity.Tts.TtsFormat;
import com.unfbx.chatgpt.entity.Tts.TtsVoice;
import com.unfbx.chatgpt.entity.whisper.WhisperResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.ResponseBody;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;


@Service
@Slf4j
@RequiredArgsConstructor
public class IAudioServiceImpl implements IAudioService {


    private final IseClientListener iseClientListener;

    private final OpenAiService openAiService;

//    private  LingoWebSocket lingoWebSocket = SpringUtils.getBean(LingoWebSocket.class);

    @Override
    public void audioEvent(String userId , byte[] bytes) {
        String dateTimeNow = DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD);
        // 构造文件路径
        String filePath = UUID.fastUUID() + ".wav";
        try {
            File file = new File(filePath);
//            if (!file.exists()) {
//                file.mkdir();
//            }
////            File file1 = new File(filePath, UUID.fastUUID() + ".pcm");
//            String fileName = filePath + UUID.fastUUID() + ".pcm";
//            File file1 = new File(fileName);
//            if (!file1.exists()){
//                file.createNewFile();
//            }
            //用户语音消息写入文件
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();
            log.info("请求openAi文本转语音:{}",filePath);
            //调用openAi
            OpenAiClient openAiClient = openAiService.getOpenAiClient(1);
//            WhisperResponse whisperResponse = openAiClient.speechToTextTranscriptions(new File(filePath));
//            whisperResponse.getText();
//            log.info("openAI语音转文本結果:{}",whisperResponse.getText());
            String text = iseClientListener.audioToText(filePath);

            String audioEvaluationRes = iseClientListener.audioEvaluation(filePath, text, null, null);
            log.info("讯飞音频评测結果:{}",audioEvaluationRes);
            TextToSpeech textToSpeech = TextToSpeech.builder()
                .model(TextToSpeech.Model.TTS_1_HD.getName())
                .input(audioEvaluationRes)
                .voice(TtsVoice.NOVA.getName())
                .responseFormat(TtsFormat.MP3.getName())
                .build();
            LingoWebSocket lingoWebSocket = SpringUtils.getBean(LingoWebSocket.class);
            ResponseBody responseBody = openAiClient.textToSpeech(textToSpeech);
            lingoWebSocket.sendBinary(userId, responseBody.bytes());
        } catch (Exception e) {
            log.error("语音消息处理错误:", e);
        }

    }
}
